﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Microsoft.Office.Interop.Word;

public partial class Police_TranslateApprove_BusinessLetter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }

    private void Bind()
    {
        if (Request["ID"] == null)
            return;
        Guid ID = new Guid(Request["ID"]);

        Yipusoft.SoldierArchives.Model.TransferTreatInfo info = Yipusoft.SoldierArchives.BLL.TransferTreat.GetDetail(ID);
        if (info == null)
            return;
        lblYearID2.Text = lblYearID.Text = info.YearNo;
        lblOldDepartment.Text=info.OldDepartment2;
        lblDepartment.Text = info.OldDepartment;
        lblEnrollmentDate.Text = info.DateArmy.Year == 9999 ? "" : info.DateArmy.ToString("yyyy年M月d日");
        lblSoldier.Text = lblName.Text = info.SoldierName;
        lblSex.Text = info.Sex;
        lblRank.Text = info.Ranger;
        lblMoveDepartment.Text = info.NewDepartment;
        lblSendDate.Text = info.DataLetter.ToString("yyyy年M月d日");
        lblApprove.Text = info.Approver;
        lblUndertake.Text = info.Undertaker;
        lblLinkman.Text = info.Linkman;
        lblRemark.Text = info.Remark;
        lblNumber2.Text = lblNumber.Text = info.Number.ToString();
        lblDate.Text = ConvertNumber(info.DateBook.ToString("yyyy年M月d日"));
    }

    private string ConvertNumber(string Number)
    {

        string[] BigNumber = new string[] { "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十", "三十一", "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        string[] SmallNumber = new string[] { "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        string ReturnStr = string.Empty;
        for (int i = 0; i < 10; i++)
        {
            Number = Number.Replace(SmallNumber[i], BigNumber[i]);
        }
        return ConvertNumber1(Number);
    }

    private string ConvertNumber1(string Number)
    {

        string[] BigNumber = new string[] { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        string[] SmallNumber = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        string ReturnStr = string.Empty;
        for (int i = 0; i < 10; i++)
        {
            Number = Number.Replace(SmallNumber[i], BigNumber[i]);
        }
        return Number;
    }

    protected void BtnExprot_Click(object sender, EventArgs e)
    {
        if (Request["ID"] == null)
            return;
        Yipusoft.SoldierArchives.Model.TransferTreatInfo info = Yipusoft.SoldierArchives.BLL.TransferTreat.GetDetail(new Guid(Request["ID"]));

        if (info == null)
            return;
        killAllProcess();
        object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word._Application oWord;
        Microsoft.Office.Interop.Word._Document oDoc;
        string FileName2 = DateTime.Now.ToString("yyyyMMddhhmmss") + ".doc";
        object Filename = @Server.MapPath("Doc/") + FileName2;
        //try
        //{
        object missing = System.Reflection.Missing.Value;
        object oTemplate = @Server.MapPath("Template/BusinessLetter.dot");
        oWord = new Microsoft.Office.Interop.Word.Application();
        oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);

        Object Nothing = System.Reflection.Missing.Value;
        object Visible = true;
        object ReadOnly = false;
        oWord.Visible = false;

        foreach (Microsoft.Office.Interop.Word.Bookmark bk in oDoc.Bookmarks)
        {
            switch (bk.Name)
            {

                case "YearID":
                    bk.Select();
                    bk.Range.Text = "[" + info.YearNo.ToString() + "]";
                    break;
                case "YearID2":
                    bk.Select();
                    bk.Range.Text = "[" + info.YearNo.ToString() + "]";
                    break;
                case "WordID":
                    bk.Select();
                    bk.Range.Text = info.Number.ToString();
                    break;
                case "WordID2":
                    bk.Select();
                    bk.Range.Text = info.Number.ToString();
                    break;
                case "Name":
                    bk.Select();
                    bk.Range.Text = info.SoldierName;
                    break;
                case "Name2":
                    bk.Select();
                    bk.Range.Text = info.SoldierName;
                    break;
                case "Sex":
                    bk.Select();
                    bk.Range.Text = info.Sex.ToString();
                    break;
                case "OldDepartment":
                    bk.Select();
                    bk.Range.Text = info.OldDepartment.ToString();
                    break;
                case "NewDepartment":
                    bk.Select();
                    bk.Range.Text = info.NewDepartment.ToString();
                    break;
                case "NewDepartment2":
                    bk.Select();
                    bk.Range.Text = info.NewDepartment.ToString();
                    break;
                case "OldDepartment2":
                    bk.Select();
                    bk.Range.Text = info.OldDepartment.ToString();
                    break;
                case "Department2":
                    bk.Select();
                    bk.Range.Text = info.OldDepartment.ToString();
                    break;
                case "LinkMan":
                    bk.Select();
                    bk.Range.Text = info.Linkman.ToString();
                    break;
                case "undertaker":
                    bk.Select();
                    bk.Range.Text = info.Undertaker.ToString();
                    break;
                case "SendDate":
                    bk.Select();
                    bk.Range.Text = info.DataLetter.ToString("yyyy年M月d日");
                    break;
                case "Ratifier":
                    bk.Select();
                    bk.Range.Text = info.Approver;
                    break;
                case "Rank":
                    bk.Select();
                    bk.Range.Text = info.Ranger;
                    break;
                case "Remark":
                    bk.Select();
                    bk.Range.Text = info.Remark;
                    break;
                case "AmyDate":
                    bk.Select();
                    bk.Range.Text = info.DateArmy.Year == 9999 ? "" : info.DateArmy.ToString("yyyy年M月d日");
                    break;
                case "LastDate":
                    bk.Select();
                    bk.Range.Text = ConvertNumber(info.DateBook.ToString("yyyy年M月d日"));
                    break;
            }


        }
        object oFormat = WdSaveFormat.wdFormatDocument97;
        oDoc.SaveAs(ref Filename, ref oFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        //}
        //catch
        //{

        //};
        // Response.Redirect("http://10.24.10.13/Police/TranslateDocument/Doc/" + FileName2.ToString());
        string path = Filename.ToString();
        FileInfo file = new FileInfo(Filename.ToString());
        FileStream myfileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        byte[] filedata = new Byte[file.Length];
        myfileStream.Read(filedata, 0, (int)(file.Length));
        myfileStream.Close();
        Response.Clear();
        Response.ContentType = "application/msword";
        Response.AddHeader("Content-Disposition", "attachment;filename=Report.doc");
        Response.Flush();
        Response.BinaryWrite(filedata);
        Response.End();
    }
    protected void killAllProcess() // 杀掉所有winword.exe进程
    {
        System.Diagnostics.Process[] myPs;
        myPs = System.Diagnostics.Process.GetProcesses();
        foreach (System.Diagnostics.Process p in myPs)
        {
            if (p.Id != 0)
            {
                string myS = "WINWORD.EXE" + p.ProcessName + "  ID:" + p.Id.ToString();
                try
                {
                    if (p.Modules != null)
                        if (p.Modules.Count > 0)
                        {
                            System.Diagnostics.ProcessModule pm = p.Modules[0];
                            myS += "\n Modules[0].FileName:" + pm.FileName;
                            myS += "\n Modules[0].ModuleName:" + pm.ModuleName;
                            myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
                            if (pm.ModuleName.ToLower() == "winword.exe")
                                p.Kill();
                        }
                }
                catch
                { }
                finally
                {
                    ;
                }
            }
        }
    }
}
